package com.longrise.co.ui.mine.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.R;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.mine.contract.MineContract;
import com.longrise.co.ui.mine.model.MineModel;
import com.longrise.co.ui.mine.model.RxModifyBackData;
import com.longrise.co.ui.mine.presenter.MinePresenter;
import com.longrise.co.utils.Constants;
import com.ly.common.base.BaseFragment;
import com.ly.common.baserx.RxBus;
import com.ly.common.commonutils.StringUtils;

import butterknife.Bind;
import butterknife.OnClick;

import static com.longrise.co.ui.mine.fragment.MyProfileFragment.RX_REAL_NAME;

/**
 * Created by luoyang on 2017-7-20.
 */

public class VerifiedFragment extends BaseFragment<MinePresenter, MineModel> implements MineContract.View<String>{

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.name)
    EditText name;
    @Bind(R.id.id_card)
    EditText idCard;
   /* @Bind(R.id.id_card_iv_up)
    ImageView idCardIvUp;
    @Bind(R.id.up_camera)
    ImageView upCamera;
    @Bind(R.id.id_card_iv_down)
    ImageView idCardIvDown;
    @Bind(R.id.down_camera)
    ImageView downCamera;*/

   private boolean isSuccess;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_verified;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this , mModel);
    }

    @Override
    protected void initView() {
        setText(headTitle, "实名认证");
    }

    public static VerifiedFragment newInstance() {
        
        Bundle args = new Bundle();
        
        VerifiedFragment fragment = new VerifiedFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @OnClick({R.id.head_back, R.id.id_card_iv_up, R.id.id_card_iv_down, R.id.submit})
    public void onViewClicked(View view) {
        if(checkDoubleClick()) {
            switch (view.getId()) {
                case R.id.head_back:
                    _mActivity.onBackPressed();
                    break;
                case R.id.id_card_iv_up:
                    break;
                case R.id.id_card_iv_down:
                    break;
                case R.id.submit:
                    try {
                        if (StringUtils.isEmpty(name.getText().toString())) {
                            showLongToast("姓名不能为空");
                            return;
                        }
                        if (StringUtils.isEmpty(idCard.getText().toString())) {
                            showLongToast("身份证号不能为空");
                            return;
                        }
                        if (StringUtils.isUsercard(idCard.getText().toString())) {
                            showLongToast("身份证号输入不合法");
                            return;
                        }
                        EntityBean bean = new EntityBean();
                        bean.set("userflag", CommonPara.getInstance().getUserFlag());
                        bean.set("realname", name.getText().toString().trim());
                        bean.set("cardno", idCard.getText().toString().trim());
                        bean.set("user", CommonPara.getInstance().getUser());
                        bean.set("sign", CommonPara.getInstance().getEnUserFlag());
                        bean.set("authstatus", "2");
                        mPresenter.modifyUserInfo(bean);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }

    }

    @Override
    public void requestSuccess(String s) {
        isSuccess = true;
        showLongToast("实名认证成功！");
        RxModifyBackData data = new RxModifyBackData();
        data.type = RX_REAL_NAME;
        data.name = name.getText().toString();
        data.cardNo = idCard.getText().toString();
        RxBus.getInstance().post(Constants.RX_EVENT_MODIFY_NICK, data);
        RxBus.getInstance().post(Constants.RX_EVENT_REFRESH_MINE, null);
    }

    @Override
    public void onRequestComplete() {
        if(isSuccess) {
            _mActivity.onBackPressed();
        }
    }
}
